package com.gz.moreaseManager.modules.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gz.moreaseManager.config.SelfUserDetails;
import com.gz.moreaseManager.core.base.BaseController;
import com.gz.moreaseManager.core.modal.AjaxResult;
import com.gz.moreaseManager.core.modal.HttpCode;
import com.gz.moreaseManager.core.modal.PageResult;
import com.gz.moreaseManager.modules.entity.Information;
import com.gz.moreaseManager.modules.service.IInformationService;
import com.gz.moreaseManager.utils.DateUtil;
import com.gz.moreaseManager.utils.UtilTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 资讯信息 前端控制器
 * </p>
 *
 * @author wike
 * @since 2019-02-26
 */
@RestController
@RequestMapping("/information")
public class InformationController extends BaseController {


    @Autowired
    private IInformationService informationService;

    @RequestMapping("list")
    public ModelAndView list(){

        return new ModelAndView("modules/information/list");
    }

    @GetMapping("add")
    public ModelAndView add(){
        return new ModelAndView("modules/information/add");
    }

    @GetMapping("update")
    public ModelAndView update(Integer id){
        Information information = informationService.selectById( id );
        ModelAndView module = new ModelAndView("modules/information/update");
        module.addObject("infor",information);
        return module;
    }

    @GetMapping("listPage")
    public PageResult<Information> findPageLsit(String name, Integer type,
                                                @DateTimeFormat(pattern = "yyyy-MM-dd")Date startDate,
                                                @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
                                                Integer pageNum, Integer pageSize){
        //分页
        Page paramsPage = new Page();
        paramsPage.setCurrent(pageNum);
        paramsPage.setSize(pageSize);
        //构建查询
        Wrapper wrapper = new EntityWrapper<Information>();
        if( UtilTool.isNotBlank( name ))
            wrapper.like("title",name);
        if( UtilTool.isNotBlank( type ))
            wrapper.eq("type",type);
        if( UtilTool.isNotBlank( startDate ) ){
            if( UtilTool.isNotBlank( endDate ) && startDate.getTime() == endDate.getTime() ){
                wrapper.between("release_time",startDate,DateUtil.addDays(startDate,1));
            }else{
                wrapper.between("release_time",startDate,
                        UtilTool.isNotBlank(endDate)?DateUtil.addDays(endDate,1):DateUtil.addDays(new Date(),1));
            }
        }
        if( UtilTool.isBlank(startDate) && UtilTool.isNotBlank( endDate )){
            wrapper.le("release_time",DateUtil.addDays(endDate,1));
        }
        //排序
        wrapper.orderBy("release_time",false);

        Page<Information> data = informationService.selectPage(paramsPage,wrapper);
        return new PageResult(0, null, data.getTotal(), data.getRecords());

    }

    /**
     * 新增品牌
     * @param information
     * @return
     */
    @PostMapping("addInformation")
    public AjaxResult<Boolean> addInformation(@RequestBody Information information ,Authentication authentication ){
        if( information == null
                || UtilTool.isBlank( information.getTitle())
                || UtilTool.isBlank( information.getType())
                || UtilTool.isBlank( information.getReleaseTime()))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");

        //创建人
        SelfUserDetails userDetails = (SelfUserDetails) authentication.getPrincipal();
        information.setCreatePerson( userDetails.getUsername());
        information.setCreateTime(new Date());
        Boolean success = informationService.insert(information);
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "新增资讯信息失败!");
    }

    /**
     * 获取资讯信息
     * @param id
     * @return
     */
    @GetMapping("findInformation")
    public AjaxResult<Information> findInformation( Integer id ){
        if( UtilTool.isBlank( id ))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");

        Information information = informationService.selectById( id );
        return AjaxResult.success(information);

    }

    /**
     * 修改资讯
     * @param information
     * @return
     */
    @PostMapping("editInformation")
    public AjaxResult<Boolean> editInformation(@RequestBody Information information){
        if( information == null
                || UtilTool.isBlank( information.getId())
                || UtilTool.isBlank( information.getTitle())
                || UtilTool.isBlank( information.getType())
                || UtilTool.isBlank( information.getReleaseTime()))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");

        /*if( UtilTool.isNotBlank( information.getUploadImg()) ){
            String imgUrl = BaseCommonUtils.base64ToFile( information.getUploadImg() );//获取上传后返回的图片
            information.setUrl( imgUrl );//上传图片返回路径
        }*/
        Boolean success = informationService.updateById(information);
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "修改资讯信息失败!");
    }

    /**
     * 根据ID删除资讯信息
     * @param id
     * @return
     */
    @PostMapping("delete")
    public AjaxResult<Boolean> delete( Integer id){

        if( UtilTool.isBlank( id ))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");

        Boolean success = informationService.deleteById( id );
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "删除资讯信息失败!");
    }

    /**
     * 时尚资讯
     * @return
     */
    @GetMapping("findInforFashion")
    public AjaxResult<List<Information>> findInforFashion(){
        List<Information> list = informationService.findInforFashion();
        if( UtilTool.isNotBlank( list ))
            return AjaxResult.success(list);
        return AjaxResult.build(HttpCode.FAILED, "获取时尚资讯信息失败!");
    }

    /**
     * 品牌资讯
     * @return
     */
    @GetMapping("findInforBrand")
    public AjaxResult<List<Information>> findInforBrand(){
        List<Information> list = informationService.findInforBrand();
        if( UtilTool.isNotBlank( list ))
            return AjaxResult.success(list);
        return AjaxResult.build(HttpCode.FAILED, "获取品牌资讯信息失败!");
    }
}
